﻿using System;
using System.Windows;
using System.Windows.Media.Imaging;
using Newtonsoft.Json.Converters;
using RadioPlayer.Properties;
using WMPLib;
using ExtensionMethods;

namespace RadioPlayer
{
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.IO;
    using Newtonsoft.Json;

    public class RadioStream 
    {
        #region Properties

        public string StreamName
        {
            get; set;
        }

        public string StreamUri
        {
            get; set;
        }

        public Guid Id { get; set; }

        public DateTime LastPlayed { get; set; }

        #endregion Properties

    }

    
    public class SettingsContainer 
    {
        
        public int Volume { get; set; }
        public RadioStream LastPlayedStation { get; set; }
        
    }
    
    public class RPlayer : INotifyPropertyChanged
    {
        #region Fields

        private ObservableCollection<RadioStream> _radioStreams;
        private WindowsMediaPlayer _wmp;
        private string _settingsfile;
        private string _streamsfile;
        private string _settingsPath;

        #endregion Fields

        #region Constructors

        public RPlayer()
        {
            Wmp = new WindowsMediaPlayer();
            //AppPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            AppPath = Environment.CurrentDirectory;
            InitializeSettings();
            SetFilePaths();
            
            string jsonsettings = File.ReadAllText(_settingsfile);
            //Settings = new SettingsContainer();
            Settings = JsonConvert.DeserializeObject<SettingsContainer>(jsonsettings);

            string jsonstreams = File.ReadAllText(_streamsfile);
            RadioStreams = new ObservableCollection<RadioStream>();
            
            RadioStreams = JsonConvert.DeserializeObject<ObservableCollection<RadioStream>>(jsonstreams);
            RadioStreams.Sort((x, y) => x.LastPlayed.CompareTo(y.LastPlayed)); 

            SelectedStationName = Settings.LastPlayedStation.StreamName;
            SelectedStationUri = Settings.LastPlayedStation.StreamUri;
            Wmp.settings.volume = Settings.Volume;
            Wmp.URL = Settings.LastPlayedStation.StreamUri;
            Wmp.controls.stop();
            GetConfiguration();

            
            
        }

        #endregion Constructors

        #region Events

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion Events

        #region Properties

        private string AppPath { get; set; }

        public SettingsContainer Settings { get; set; }

        public bool IsMuted
        {
            get; set;
        }

        public bool IsPlaying
        {
            get; set;
        }

        public ObservableCollection<RadioStream> RadioStreams 
        {
            get { return _radioStreams; } set { _radioStreams = value; OnPropertyChanged(new PropertyChangedEventArgs("RadioStreams")); }
        }

        public WindowsMediaPlayer Wmp
        {
            get { return _wmp; }
            set { _wmp = value; OnPropertyChanged(new PropertyChangedEventArgs("Wmp")); }
        }

        public string SelectedStationName
        {
            get; set;
        }

        public string SelectedStationUri
        {
            get; set;
        }

        public BitmapImage SetMuteButtonImage(bool isMuted)
        {
            var bitmapImage = (isMuted)
                                  ? new BitmapImage(new Uri(@"Images\mute.png", UriKind.Relative))
                                  : new BitmapImage(new Uri(@"Images\unmute.png", UriKind.Relative));

            return bitmapImage;
        }

        public BitmapImage SetPlayButtonImage(bool isPlaying)
        {
            var bitmapImage = isPlaying ? new BitmapImage(new Uri(@"Images\stop.png", UriKind.Relative)) : new BitmapImage(new Uri(@"Images\play.png", UriKind.Relative));
            return bitmapImage;
        }
        public int Volume { get; set; }
        #endregion Properties

        #region Methods
        public void InitializeSettings()
        {
            if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\mRadio"))
            {
                Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\mRadio");
            }

            _settingsPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\mRadio";

            //_settingsfile = CreateSettingsFile();
            //_streamsfile = CreateStreamsFile();
        }

        public void SetFilePaths()
        {

            if (!File.Exists(_settingsPath + @"\\settings.json"))
            {
                File.Copy(@"settings.json", _settingsPath + @"\\settings.json");
            }

            _settingsfile = _settingsPath + @"\settings.json";


            if (!File.Exists(_settingsPath + @" \\streams.json"))
            {
                File.Copy(@"streams.json", _settingsPath + @"\\streams.json");
            }

            _streamsfile = _settingsPath + @"\streams.json";
        }

        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, e);
            }
        }

        public void WriteStreamsToJson()
        {
            string json = JsonConvert.SerializeObject(RadioStreams, Formatting.Indented);
            if (json != File.ReadAllText(_streamsfile))
            {
                File.WriteAllText(_streamsfile, json);
            }

            string jsonsettings = JsonConvert.SerializeObject(Settings, Formatting.Indented);
            if (jsonsettings!= File.ReadAllText(_settingsfile))
            {
                File.WriteAllText(_settingsfile, jsonsettings);
            }
        }

        public void WriteStreamsToSettings()
        {
            Settings.LastPlayedStation.StreamName = SelectedStationName;
            Settings.LastPlayedStation.StreamUri = SelectedStationUri;
            Settings.Volume = _wmp.settings.volume;
        }

        public void GetConfiguration()
        {
            SelectedStationName = Settings.LastPlayedStation.StreamName;
            SelectedStationUri = Settings.LastPlayedStation.StreamUri;
        }

        public void SetConfiguration()
        {
            Settings.LastPlayedStation = new RadioStream { StreamName = SelectedStationName, StreamUri = SelectedStationUri };
            Settings.Volume = this._wmp.settings.volume;
        }

        #endregion Methods

        #region Commands

        #endregion
    }
}